<?php
/**
 * @title 825. 适龄的朋友
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $ages
     * @return Integer
     */
    function numFriendRequests($ages) {
        /**
         * @since 2024-01-19 排序
         */
        sort($ages);
        // echo "ages=", implode(",", $ages), "\n";

        /**
         * @since 2024-01-19 数组数量
         */
        $agesCount = count($ages);

        /**
         * @since 2024-01-19 好友请求数量
         */
        $requestCount = 0;

        /**
         * @since 2024-01-19 遍历数组
         */
        $prevAge = $prevCount = $prevRequest = $s = 0;
        for($i=0; $i<$agesCount; $i++){
            /**
             * @since 2024-01-19 下述任意一个条件为真，那么用户 x 将不会向用户 y（x != y）发送好友请求
             * ages[y] <= 0.5 * ages[x] + 7
             * ages[y] > ages[x]
             * ages[y] > 100 && ages[x] < 100
             *
             * 3个条件为真
             * 1. ages[y] > 0.5*ages[x]+7
             * 2. ages[y] <= ages[x]
             * 3. ages[y] <= 100 || ages[x] >= 100
             */

            /**
             * @since 2024-01-19 用户x的年龄
             */
            $x = $ages[$i];

            /**
             * @since 2024-01-19 x<100, 保证0.5*x+7 < y <=x，满足条件1和2，因为y<=x同时满足条件3， y<=x<100
             * @since 2024-01-19 x>=100，条件3满足，只需要保证 0.5*x+7 < y <=x，满足条件1和2
             * 不管x多少，0.5*x+7 < y <=x，即可, 0.5*x+7 < x, 14 < x
             * 数组排序，y=x，单独处理
             */
            if($x < 15){
                continue;
            } else {}

            if($x == $prevAge){
                $prevCount++;
                $requestCount += $prevRequest;
                continue;
            } else {
                /**
                 * @since 2024-01-19 处理上次相同年龄的好友请求
                 */
                if($prevCount>=2){
                    $requestCount += ($prevCount-1)*$prevCount;
                    // echo "x==y, age={$prevAge}, request=", ($prevCount-1)*$prevCount, "\n";
                } else {}
                $prevAge = $x;
                $prevCount = 1;

                /**
                 * @since 2024-01-19 y > 0.5*x+7
                 */
                $minY = 0.5*$x + 7;

                /**
                 * @since 2024-01-19 二分查找
                 */
                // $s = 0;
                $e = $i-1;
                while($s<=$e){
                    $m = ($s+$e)>>1;
                    $v = $ages[$m];

                    if($v>$minY){
                        $e = $m-1;
                    } elseif($v<=$minY) {
                        $s = $m+1;
                    } else {}
                }
                $xRequest = $i-$s;
                $prevRequest = $xRequest;
                $requestCount += $xRequest;
                echo "i={$i}, x={$x}, request={$xRequest}, minY={$minY}, s={$s}, requestCount={$requestCount}\n";
            }
        }

        /**
         * @since 2024-01-19 收官
         */
        if($prevCount >= 2){
            $requestCount += $prevCount*($prevCount-1);
            // echo "x==y, age={$prevAge}, request=", ($prevCount-1)*$prevCount, "\n";
        } else {}

        /**
         * @return Integer
         */
        return $requestCount;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.19_7.php
 */
$datas = [
    // [[20,30,100,110,120]], // 3
    [[54,23,102,90,40,74,112,74,76,21]], // 22
    // [[73,106,39,6,26,15,30,100,71,35,46,112,6,60,110]], // 29
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->numFriendRequests(...$data);
    echo $xhprof->DumpArgs([$result]);
    // var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
